//
//  tapburstfreeAppDelegate.cpp
//  tapburstfree
//
//  Created by Limin on 12-9-4.
//  Copyright HappyBluefin 2012年. All rights reserved.
//

#include "PreHeader.h"
#include "AppDelegate.h"
#include "LayerLogo.h"
#include "LayerMain.h"
#include "HBScore.h"
#include "DataSL.h"

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    TargetPlatform target = getTargetPlatform();
    switch (target)
    {
    case kTargetIpad:
        // ipad
        
        CCFileUtils::sharedFileUtils()->setResourceDirectory("imageshd");
        
        // don't enable retina because we don't have ipad hd resource
        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(768, 1024, kResolutionNoBorder);
        break;

    case kTargetIphone:
        // iphone

        // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
        if (pDirector->enableRetinaDisplay(true))
        {
            // iphone hd
            CCFileUtils::sharedFileUtils()->setResourceDirectory("imageshd");
        }
        else 
        {
            CCFileUtils::sharedFileUtils()->setResourceDirectory("images");
        }

        break;

    default:
        // android, windows, blackberry, linux or mac
        // use 960*640 resources as design resolution size
        CCFileUtils::sharedFileUtils()->setResourceDirectory("imageshd");
        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640, 960, kResolutionShowAll);

//        CCEGLView *view = CCEGLView::sharedOpenGLView();
//        CCSize size = view->getFrameSize();
//        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(size.width, size.height, kResolutionShowAll);
//        CCEGLView::sharedOpenGLView()->setDesignResolutionSize(320, 480, kResolutionExactFit);

        break;
    }

    // turn on display FPS
//    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    HBScore::initScore();

    // Sound
    SimpleAudioEngine::sharedEngine()->setEffectsVolume(1.0);

    DataSL::load();

    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile("sheet.plist");

    // create a scene. it's an autorelease object
    CCScene *pScene =  LayerLogo::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->stopAnimation();
    CCDirector::sharedDirector()->pause();

    // if you use SimpleAudioEngine, it must be pause
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->resume();
    CCDirector::sharedDirector()->startAnimation();
    
    // if you use SimpleAudioEngine, it must resume here
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
